package List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * 迭代集合的物种方法
 * 1.toArray()
 */
public class ListIteratorDemo2 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("下标1");
        list.add("下标2");
        list.add("下标2");
        list.add("下标4");
        System.out.println("使用toArray()函数遍历集合");
        Object[] arr = list.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        System.out.println("使用Iterator迭代器遍历集合");
        for(Iterator it=list.iterator(); it.hasNext();) {
            System.out.println(it.next());
        }
        System.out.println("使用ListIterator迭代器遍历集合");
        for(ListIterator it = list.listIterator(); it.hasNext();) {
            System.out.println(it.next());
        }
        System.out.println("使用for循环遍历集合");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("使用foreach遍历集合");
        for (Object obj : list) {
            System.out.println(obj);
        }
    }
}
